package com.genghis.ptas.userfriends.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.scorm.entity.ScormInfo;
import com.genghis.ptas.userfriends.dao.UserFriendDao;
import com.genghis.ptas.userfriends.entity.UserFriend;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *
 * @author yutong
 * @version 1.0.1
 */

@Repository("userFriendDao")
public class UserFriendDaoImpl extends PageDao implements UserFriendDao {


    @Override
    public Page<UserFriend> findFriendPageList(PageParameter pageParameter, UserFriend userFriend) {
        StringBuilder sql= new StringBuilder("SELECT f.id,f.friend_id,u.login_name,u.sex,u.birthday,u.email,u.city  FROM user_friends f,user_info u WHERE f.friend_id=u.id AND f.user_id="+userFriend.getUserId());
        if (userFriend.getLoginName() != null && !userFriend.getLoginName().equals("")) {
            sql.append(" AND u.login_name like '%").append(userFriend.getLoginName().trim()).append("%'");
        }
        return queryForPage(pageParameter,sql.toString(),new BeanPropertySqlParameterSource(userFriend),new BeanPropertyRowMapper<UserFriend>(UserFriend.class));
    }

    @Override
    public int delAllReply(int postId) {
        String sql="DELETE FROM post_reply WHERE post_id=?" ;
        return getJdbcTemplate().update(sql,postId);
    }

    @Override
    public int delOneReplyById(int id) {
        String sql="DELETE FROM post_reply WHERE id=?";
        return getJdbcTemplate().update(sql,id);
    }

    @Override
    public int addFriend(UserFriend userFriend) {
        String sql = "INSERT INTO user_friends(user_id, friend_id) VALUES(:userId,:friendId)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(userFriend), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public int delFriendById(int id) {
        String sql="DELETE FROM user_friends WHERE id=?";
        return getJdbcTemplate().update(sql,id);
    }

    @Override
    public List<String> findFriendId(int id) {
        String sql="SELECT f.`friend_id` fId  FROM user_friends f WHERE f.`user_id`="+id;
        return getJdbcTemplate().queryForList(sql,String.class);
    }

    @Override
    public Page<ScormInfo> findFriendCoursePage(PageParameter pageParameter, ScormInfo scormInfo, int userId) {
        StringBuilder sql=new StringBuilder("SELECT  c.scorm_id sormId, c.user_id, s.*, u.login_name  FROM "+
                " user_course_info c, scorm_info s, user_info u WHERE c.scorm_id = s.id AND u.id = c.user_id"+
                " AND c.user_id IN (SELECT f.`friend_id` friendId FROM user_friends f WHERE f.`user_id` = "+userId
                +")ORDER BY c.user_id ");
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(scormInfo), new BeanPropertyRowMapper<ScormInfo>(ScormInfo.class)) ;

    }
}